<template>
  <div class="app-content-wrap" :style="'minHeight:' + gHeight + 'px;'">
    <div class="g-content-title">
      用户信息
    </div>
    <!-- 搜索框 -->
    <el-form :model="userFilter" label-width="80px" size="small">
      <el-row :gutter="15">
        <el-col :span="4">
          <el-form-item label="姓名">
            <el-input v-model="userFilter.userName" placeholder="姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="电话">
            <el-input v-model="userFilter.mobile" placeholder="电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="是否认证">
            <el-select v-model="userFilter.proveState" placeholder="是否认证">
              <el-option v-for="item in subTypeArr" :key="item.value" :value="item.value" :label="item.name">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="所属小区">
            <AreaSelect :selectModel="userFilter.deptId" :areaIds="areaIds" @onAreaChange="onAreaChange" hideThird="true">
            </AreaSelect>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="primary" size="small" icon="el-icon-search" @click="getUserFromFilter">搜 索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 列表 -->
    <div style="margin:10px 30px;">
      <el-table :data="userListTable" border style="width: 100%">
        <el-table-column label="头像" min-width="120" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.wechatImger" :alt="scope.row.wechatName" width="50" height="50" />
          </template>
        </el-table-column>
        <el-table-column prop="wechatName" label="昵称" min-width="100" align="center"></el-table-column>
        <el-table-column prop="mobile" label="电话" min-width="140" align="center"></el-table-column>
        <el-table-column prop="deptList" label="小区" min-width="140" :formatter="transformDeptName"></el-table-column>
        <el-table-column label="状态" min-width="100" align="center">
          <template slot-scope="scope">
            <div>
              <el-tag type="danger" size="small" v-if="scope.row.status === '2'">黑名单</el-tag>
              <el-tag type="success" size="small" v-else>正常</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="190" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="userDetail(scope.row)">详情</el-button>
            <el-button type="text" @click="addBlackList(scope.row)">黑名单</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination background @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size="10" layout="total, prev, pager, next,jumper" :total="pagination.total" class="el-page-body"></el-pagination>
    </div>
  </div>
</template>
<script>
import AreaSelect from '../../components/common/select/area.vue'
// import areaSelect from '../../components/common/select/area'
// import SelectRole from '../../components/common/select/role.vue'

export default {
  name: 'userInfo',
  components: {
    AreaSelect
  },
  data () {
    return {
      gHeight: this.$store.state.gHeight,
      subTypeArr: [],
      userFilter: {
        userName: '', // 姓名
        mobile: '', // 电话
        deptId: '', // 部门
        proveState: 0 // 是否认证0:否 1：是
      },
      areaIds: {
        areaId1: '', // 所属1级
        areaId2: '', // 所属2级
        areaId3: '' // 所属3级
      },
      userListTable: [],
      pagination: {
        currentPage: 1,
        total: null
      }
    }
  },
  methods: {
    transformDeptName (row) {
      let depts = []
      row.deptList.forEach((item) => {
        depts.push(item.deptName)
      })
      return depts.join('，')
    },
    addUser () {
      this.$router.push({
        name: 'userModify',
        params: {
          userId: 'addUser'
        }
      })
    },
    userDetail: function (row) {
      this.$router.push({
        name: 'appletUserDetail',
        params: {
          userId: row.userId
        }
      })
    },
    // 加如黑名单
    addBlackList (row) {
      let userId = row.userId
      this.$confirm('添加黑名单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleAddBlackList(userId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消添加黑名单'
        })
      })
    },
    handleAddBlackList (userId) {
      const url = this.$base.getUrl('appletuserDis')
      this.$store.dispatch('postForm', {
        isJson: true,
        url: url,
        data: {
          userId: userId
        }
      }).then((res) => {
        if (res.code === 0) {
          this.$message({
            type: 'success',
            message: '添加黑名单成功!'
          })
          this.getUser()
        }
      })
    },
    userModify (row) {
      this.$router.push({
        name: 'userModify',
        params: {
          userId: row.userId
        }
      })
    },
    getUserFromFilter: function () { /// /表单
      this.pagination.currentPage = 1
      this.getUser()
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
      this.getUser()
    },
    getUser () {
      const url = this.$base.getUrl('appletGetList')
      let params = {
        isJson: true,
        url: url,
        data: {
          userName: this.userFilter.userName, // 姓名
          mobile: this.userFilter.mobile, // 电话
          deptId: this.userFilter.deptId, // 部门
          proveState: this.userFilter.proveState, // 是否认证0:否 1：是
          page: this.pagination.currentPage,
          limit: 10
        }
      }
      this.$store.dispatch('postForm', params).then((res) => {
        let { result } = res
        this.userListTable = result.list
        this.pagination.currentPage = result.currPage
        this.pagination.total = result.totalCount
      })
    },
    // role组件change时
    onRoleChange (val) {
      this.userFilter.role = val
    },
    // 所属变化时
    onAreaChange (val) {
      this.userFilter.deptId = val
    }
  },
  mounted () {
    this.subTypeArr = this.$base.getConst('select', 'proveState')
    this.getUser()
  },
  beforeRouteLeave (to, from, next) {
    if (to.name !== 'userDetail' && to.name !== 'userModify') {
      // if (to.name !== 'userDetail') {
      this.$store.commit('keepOut')
    }
    next()
  },
  beforeRouteEnter (to, from, next) {
    next((vm) => { })
  }
}
</script>
<style scoped>
.el-table {
  width: auto !important;
  text-align: center;
  margin: 10px;
}

.el-table >>> th {
  text-align: center;
}
</style>
